package edu.nctu.csie.jichang.database.app;

import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.nctu.csie.jichang.database.dbinfo.DataBaseInfo;
import edu.nctu.csie.jichang.database.dbinfo.LoginInfo;
import edu.nctu.csie.jichang.database.model.cell.DBDatabase;
import edu.nctu.csie.jichang.database.parser.IParser;
import edu.nctu.csie.jichang.database.parser.ParseDML;
import edu.nctu.csie.jichang.database.save.SaveSQLFile;
import edu.nctu.csie.jichang.database.save.SaveXML;

public class DBBackup {
	
	private static final Logger LOG = LoggerFactory.getLogger(DBBackup.class);
	
	/**
	 * @param pArgs
	 */
	public static void main(String[] pArgs)throws Exception {
		
		LOG.info("Strating - Database Backup ...");
		ApplicationContext tContext = new ClassPathXmlApplicationContext("dbtools.xml");
		DataBaseInfo tCurrentDatabase = tContext.getBean("backup",DataBaseInfo.class);

		List<LoginInfo> tLoginInfos = tCurrentDatabase.getLoginInfos();

		IParser tParser = new ParseDML();
		DBDatabase tCurrent = tParser.doParse(tLoginInfos);
		tCurrent.setDatabaseName("data");
		
		new SaveSQLFile().doSave(new File("output/data.sql"), tCurrent);
		
		new SaveXML().doSave(new File("output/data.xml"), tCurrent);
		
		LOG.info("Ending - Database Backup ...");
		
	}
	
}